package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.HolidaySettingConstants;
import com.ruoyi.common.enums.GlobalSettingsType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.GeneralConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GeneralConfigurationMapper;
import com.ruoyi.system.service.IGeneralConfigurationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 假期设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-25
 */
@Service
public class GeneralConfigurationServiceImpl implements IGeneralConfigurationService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private GeneralConfigurationMapper generalConfigurationMapper;

    /**
     * 判断是否存在type这个假期设置项
     * @param type
     * @return
     */
    @Override
    public GeneralConfiguration selectHolidaySettingsByType(String type) {
        return generalConfigurationMapper.selectHolidaySettingsByType(type);
    }

    /**
     * 根据类型获取类型值
     * @param type-
     * @return
     */
    @Override
    public String selectHolidaySettingByType(String type) {
        return generalConfigurationMapper.selectHolidaySettingByType(type);
    }

    /**
     * 查询假期设置
     * 
     * @param id 假期设置主键
     * @return 假期设置
     */
    @Override
    public GeneralConfiguration selectHolidaySettingsById(Long id)
    {
        return generalConfigurationMapper.selectHolidaySettingsById(id);
    }

    /**
     * 查询假期设置列表
     * 
     * @param generalConfiguration 假期设置
     * @return 假期设置
     */
    @Override
    public List<GeneralConfiguration> selectHolidaySettingsList(GeneralConfiguration generalConfiguration)
    {
        List<GeneralConfiguration> generalConfigurationList = generalConfigurationMapper.selectHolidaySettingsList(generalConfiguration);
        generalConfigurationList.forEach(settings -> settings.setDescription(HolidaySettingConstants.convertType(settings.getType())));
        return generalConfigurationList;
    }

    /**
     * 新增假期设置
     * 
     * @param generalConfiguration 假期设置
     * @return 结果
     */
    @Override
    public int insertHolidaySettings(GeneralConfiguration generalConfiguration)
    {
        return generalConfigurationMapper.insertHolidaySettings(generalConfiguration);
    }

    /**
     * 修改假期设置
     * 
     * @param generalConfiguration 假期设置
     * @return 结果
     */
    @Override
    public int updateHolidaySettings(GeneralConfiguration generalConfiguration)
    {
        return generalConfigurationMapper.updateHolidaySettings(generalConfiguration);
    }

    /**
     * 批量删除假期设置
     * 
     * @param ids 需要删除的假期设置主键
     * @return 结果
     */
    @Override
    public int deleteHolidaySettingsByIds(Long[] ids)
    {
        return generalConfigurationMapper.deleteHolidaySettingsByIds(ids);
    }

    /**
     * 删除假期设置信息
     * 
     * @param id 假期设置主键
     * @return 结果
     */
    @Override
    public int deleteHolidaySettingsById(Long id)
    {
        return generalConfigurationMapper.deleteHolidaySettingsById(id);
    }

    /**
     * 导入假期设置
     * @param generalConfigurationList
     * @param isUpdateSupport
     */
    @Override
    @Transactional
    public void importHolidaySettings(List<GeneralConfiguration> generalConfigurationList, boolean isUpdateSupport) {
        if (StringUtils.isNull(generalConfigurationList) || generalConfigurationList.size() == 0)
        {
            throw new ServiceException("导入假期数据不能为空！");
        }
        for (GeneralConfiguration generalConfiguration : generalConfigurationList)
        {
            if(!StringUtils.isNull(generalConfiguration.getTypeValue())){
                try
                {
                    // 验证是否存在这个假期设置
                    GeneralConfiguration h = generalConfigurationMapper.selectHolidaySettingsByType(generalConfiguration.getType());
                    if (StringUtils.isNull(h))
                    {
                        this.insertHolidaySettings(generalConfiguration);
                    }else if (isUpdateSupport)
                    {
                        h.setTypeValue(generalConfiguration.getTypeValue());
                        this.updateHolidaySettings(h);
                    }
                }
                catch (Exception e)
                {
                    throw new GlobalException("导入的数据可能已存在");
                }
            }else{
                throw new GlobalException("假期设置表不完善,无法导入!");
            }
        }
    }

    @Override
    @Transactional
    public int saveAttendanceSetting(Map<String, Object> map) {
        //考勤时间
        String attendanceTime = map.get("attendanceTime") + "";
        //考勤时间段
        String morningAttendanceStartTime = (String) map.get("morningAttendanceStartTime");
        String morningAttendanceEndTime = (String) map.get("morningAttendanceEndTime");
        String afternoonAttendanceStartTime = (String) map.get("afternoonAttendanceStartTime");
        String afternoonAttendanceEndTime = (String) map.get("afternoonAttendanceEndTime");
        //查询数据库中是否有这些记录，有则替换没有则插入
        handleSaveAttendance(attendanceTime,GlobalSettingsType.ATTENDANCE_TIME.getType());
        handleSaveAttendance(morningAttendanceStartTime,GlobalSettingsType.MORNING_ATTENDANCE_START_TIME.getType());
        handleSaveAttendance(morningAttendanceEndTime,GlobalSettingsType.MORNING_ATTENDANCE_END_TIME.getType());
        handleSaveAttendance(afternoonAttendanceStartTime,GlobalSettingsType.ATTENDANCE_AFTERNOON_START_TIME.getType());
        int i = handleSaveAttendance(afternoonAttendanceEndTime, GlobalSettingsType.AFTERNOON_ATTENDANCE_END_TIME.getType());
        return i;
    }

    @Override
    public List<GeneralConfiguration> selectAttendanceList() {
        return generalConfigurationMapper.selectAttendanceList();
    }

    public int handleSaveAttendance(String time,String type){
        GeneralConfiguration g = generalConfigurationMapper.selectHolidaySettingsByType(type);
        if(StringUtils.isNull(g)){
            GeneralConfiguration generalConfiguration = new GeneralConfiguration();
            generalConfiguration.setType(type);
            generalConfiguration.setTypeValue(time);
            int i = generalConfigurationMapper.insertHolidaySettings(generalConfiguration);
            return i;
        }else{
            g.setTypeValue(time);
            int i = generalConfigurationMapper.updateHolidaySettings(g);
            return i;
        }
    }
}
